#define DEBUG
#include <cstdio>

using namespace std;

const int maxn=100000, maxk=17, inf=0x7ffffff;

int min(int a, int b) {
  return a<b ? a : b;
}

void initFG(int n, int f[][maxk+1], int g[][maxk+1]) {
  for (int i=1; i<=maxk; i++) {
    for (int j=1; j<=n; j++) {
      f[i][j] = f[f[i][j-1]][j-1];
      g[i][j] = min(g[i][j-1], g[f[i][j-1]][j-1]);
    }
  }
}

void initDep(int n, int *dep, int f[][maxk+1], bool *son) {
  static int que[maxn+1];
  static bool book[maxn+1];
  for (int i=1; i<=n; i++) {
    if (son[i]) {
      que[++que[0]] = i;
    }
  }
  for (int h=0; h<que[0]; ) {
    h++;
    if (!book[f[que[h]][0]]) {
      que[++que[0]] = f[que[h]][0];
      book[f[que[h]][0]] = true;
    }
  }
  for (int i=que[0]-1; i>0; i--) {
    dep[que[i]] = dep[f[que[i]][0]]+1;
  }
}

int main() {
  freopen("money.in", "r", stdin);
  freopen("money.out", "w", stdout);

  int n, m, lastans=0;
  scanf("%d %d", &n, &m);

  bool first=true, deal=false;
  static int f[maxn+1][maxk+1], g[maxn+1][maxk+1], dep[maxn+1];
  static bool son[maxn+1];
  for (int i=1; i<=m; i++) {
    int type;
    scanf("%d", &type);
    if (type==0) {
      int a, b, c;
      scanf("%d %d %d", &a, &b, &c);
      a = (a + lastans) % n + 1;
      b = (b + lastans) % n + 1;
      c = (c + lastans) % n + 1;

      son[b] = true;
      f[a][0] = b;
      g[a][0] = c;
      if (deal) {
	first = false;
      }
    } else {
      int a, b;
      scanf("%d %d", &a, &b);
      a = (a + lastans) % n + 1;
      b = (b + lastans) % n + 1;

      lastans = inf;
      if (!deal) {
	initFG(n, f, g);
	initDep(n, dep, f, son);
	deal = true;
      }
      if (first) {
	for (int j=maxk; j>=0; j--) {
	  if (dep[f[a][j]]>=dep[b]) {
	    lastans = min(g[a][j], lastans);
	    a = f[a][j];
	  }
	}
      } else {
	for (; a!=b && a; a=f[a][0]) {
	  lastans = min(g[a][0], lastans);
	}
      }
      if (a!=b) {
	lastans = 0;
      }
      printf("%d\n", lastans);
    }
  }

  fclose(stdin);
  fclose(stdout);
  return 0;
}
